package com.lin.controller.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.lin.commons.Result;
import com.lin.dto.UserExtraCostDTO;
import com.lin.entity.CarInformation;
import com.lin.entity.CarUserBill;
import com.lin.entity.UserExtraCost;
import com.lin.mapper.UserExtraCostMapper;
import com.lin.service.CarInformationService;
import com.lin.service.CarUserBillService;
import com.lin.service.UserExtraCostService;
import com.lin.vo.DataVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Created by Lin Weihong
 * @Time on 2021/11/28 19:24
 */
@RestController
@CrossOrigin
@RequestMapping("/admin")
public class AdminCarsReturnInformation {

    @Autowired
    UserExtraCostService userExtraCostService;
    @Autowired
    CarUserBillService carUserBillService;
    @Autowired
    CarInformationService carInformationService;

    @GetMapping("/allReturnInfo")
    public DataVO<UserExtraCost> getAllReturnInfo(Integer page,Integer limit) {
        return userExtraCostService.getAllUserExtraCost(page, limit);
    }

    @PostMapping("/modifyReturnInfo")
    public Result modifyReturnInfo(@RequestBody UserExtraCostDTO userExtraCostDTO) {
        int result = userExtraCostService.modifyReturnPrice(userExtraCostDTO);
        if (result == 1) {
            return Result.success("通知成功");
        }else {
            return Result.failure("通知失败");
        }
    }

    @PostMapping("/agree/return")
    @Transactional
    public Result agreeReturn(@RequestParam("orderSerial") String orderSerial){
        System.out.println(orderSerial);
        CarUserBill carUserBill = carUserBillService.getById(orderSerial);
        Integer carId = carUserBill.getCarId();
        CarInformation carInformation = carInformationService.getById(carId);
        carInformation.setStock(carInformation.getStock()+1);
        carUserBillService.removeById(orderSerial);
        carInformationService.updateById(carInformation);
        return Result.success("同意还车成功");
    }

}
